<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>流程控制的替代语法</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="control-structures.elseif.html">elseif/else if</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="control-structures.while.html">while</a></div>
 <div class="up"><a href="language.control-structures.html">流程控制</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="control-structures.alternative-syntax" class="sect1">
 <h2 class="title">流程控制的替代语法</h2>
 <p class="verinfo">(PHP 4, PHP 5)</p>
 <p class="para">
  PHP 提供了一些流程控制的替代语法，包括
  <em>if</em>，<em>while</em>，<em>for</em>，<em>foreach</em>
  和 <em>switch</em>。替代语法的基本形式是把左花括号（{）换成冒号（:），把右花括号（}）分别换成
  <em>endif;</em>，<em>endwhile;</em>，<em>endfor;</em>，<em>endforeach;</em>
  以及 <em>endswitch;</em>。
  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">):&nbsp;</span><span style="color: #0000BB">?&gt;<br /></span>A&nbsp;is&nbsp;equal&nbsp;to&nbsp;5<br /><span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #007700">endif;&nbsp;</span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </p>
 <p class="simpara">
  在上面的例子中，HTML 内容“A is equal to 5”用替代语法嵌套在
  <em>if</em> 语句中。该 HTML 的内容仅在
  <var class="varname"><var class="varname">$a</var></var> 等于 5 时显示。
 </p>
 <p class="para">
  替代语法同样可以用在 <em>else</em> 和
  <em>elseif</em> 中。下面是一个包括
  <em>elseif</em> 和 <em>else</em>
  的 <em>if</em> 结构用替代语法格式写的例子：
  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">):<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"a&nbsp;equals&nbsp;5"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"..."</span><span style="color: #007700">;<br />elseif&nbsp;(</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">6</span><span style="color: #007700">):<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"a&nbsp;equals&nbsp;6"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"!!!"</span><span style="color: #007700">;<br />else:<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"a&nbsp;is&nbsp;neither&nbsp;5&nbsp;nor&nbsp;6"</span><span style="color: #007700">;<br />endif;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </p>
 <blockquote class="note"><p><strong class="note">Note</strong>: 
  <p class="para">
   不支持在同一个控制块内混合使用两种语法。
  </p>
 </p></blockquote>
 <p class="para">
  更多例子参见 <a href="control-structures.while.html" class="link">while</a>，<a href="control-structures.for.html" class="link">for</a> 和
  <a href="control-structures.if.html" class="link">if</a>。
 </p>
</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="control-structures.elseif.html">elseif/else if</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="control-structures.while.html">while</a></div>
 <div class="up"><a href="language.control-structures.html">流程控制</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
